﻿using Microsoft.AspNetCore.Mvc;
using MVC系统mvc.Dto.IngredientDto;
using MVC系统mvc.Dto.Quality;
using Newtonsoft.Json;
using WMS_MAV;

namespace MVC系统mvc.Controllers
{
    /// <summary>
    /// 原料控制器
    /// </summary>
    public class IngredientController : Controller
    {
        private readonly IConfiguration _configuration;

        public IngredientController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }
        //原料添加视图
        public IActionResult AddIngredient()
        {
            var apiUrl = _configuration["Url"];
            return View(new { ApiUrl = apiUrl });
           
        }
        /// <summary>
        /// 添加原料
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> IngredientAdd(IngredientAddDto model)
        {
            HttpClientWrapper client = new HttpClientWrapper(model.token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/AddIngredient";
            string list = JsonConvert.SerializeObject(model);
            var response = await client.PostAsync(url, list);
            return Json(response);
        }
        //原料显示视图
        public IActionResult ShowIngredient()
        {
            return View();
        }
        /// <summary>
        /// 原料显示
        /// </summary>
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> IngredientShow(IngredientShowDto model)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/Show?Pageindex="+model.Pageindex+"&Pagesize="+model.Pagesize+"&Ingredientnumber="+model.Ingredientnumber+"&IngredientName="+model.IngredientName+"&State="+model.State;
            var datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 质检组单删
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> IsDelDo(string model, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/DelIngredient";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);
        }
        //原料修改视图
        public IActionResult UpdateIngredient()
        {
            var apiUrl = _configuration["Url"];
            return View(new { ApiUrl = apiUrl });

        }
        /// <summary>
        /// 原料反填
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> IngredientFanDo(IngredientBackfillDto model)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/BackfillIngredient?IngredientId="+model.IngredientId;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }
        /// <summary>
        /// 原料管理修改
        /// </summary>
        /// <param name="models"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> IngredientUodate(IngredientAddDto obj)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(obj.token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/UpdateIngredient";
            string list = JsonConvert.SerializeObject(obj);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }
        /// <summary>
        /// 修改原料管理状态
        /// </summary>
        /// <param name="model"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> IsUpdateIngredientSarte(IngredentStateDto model)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(model.token);
            string url = _configuration["Url"] + "yuan/api/Ingredient/UpdateIngredientState?IngredientId="+model.IngredientId+"&State="+model.State;
            string list = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url, list);
            return Json(response);
        }
        //原料查看视图
        public IActionResult LookIngredient()
        {
            var apiUrl = _configuration["Url"];
            return View(new { ApiUrl = apiUrl });
        }

    }

}
